package com.example.qixin0314;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.qixin0314.Nat.BaseApplication;
import com.example.qixin0314.fragment.followFragment;
import com.example.qixin0314.fragment.homeFragment;
import com.example.qixin0314.fragment.mineFragment;
import com.example.qixin0314.fragment.newsFragment;
import com.example.qixin0314.fragment.stateFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

import cn.leancloud.LCUser;

public class OneActivity extends AppCompatActivity {
    private BottomNavigationView homeNavi;
    private List<Fragment> mHomeFragments = new ArrayList<>();
    public FragmentTransaction ft;
    private homeFragment fragment;
    private int itemId;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_one);
        initView();//控件初始化
        Click();

    }

    private void initView() {//控件初始化
        homeNavi = findViewById(R.id.home_navi);
        fragment = new homeFragment();

        mHomeFragments.add(fragment);
        mHomeFragments.add(new stateFragment());
        mHomeFragments.add(new newsFragment());
        mHomeFragments.add(new followFragment());
        mHomeFragments.add(new mineFragment());

        ft = getSupportFragmentManager().beginTransaction();
        for (int i = 0; i < mHomeFragments.size(); i++) {
            ft.add(R.id.home_container, mHomeFragments.get(i));

            if (i != 0) {
                ft.hide(mHomeFragments.get(i));
            }
        }
        ft.commit();
        //控制getSupportFragmentManager().beginTransaction().show()/hide()
        homeNavi.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                itemId = item.getItemId();
                replaceHomeFragment(itemId);

                return true;
            }
        });
    }

    private void replaceHomeFragment(int id)//for循环，控制fragment切换
    {
        ft = getSupportFragmentManager().beginTransaction();

        for (int i = 0; i < mHomeFragments.size(); i++) {
            ft.hide(mHomeFragments.get(i));
            switch (id) {
                case R.id.btn_home:
                    ft.show(mHomeFragments.get(0));
                    break;

                case R.id.btn_stare:
                    ft.show(mHomeFragments.get(1));
                    break;
                case R.id.btn_news:
                    ft.show(mHomeFragments.get(2));
                    break;
                case R.id.btn_follow:{
                    LCUser currentUser = LCUser.getCurrentUser();
                    if (currentUser != null) {
                        ft.show(mHomeFragments.get(3));
                        break;
                    } else {
                        Intent intent = new Intent(OneActivity.this, LoginActivity.class);
                        startActivity(intent);
                        break;
                    }
                }
                case R.id.btn_mine:
                    ft.show(mHomeFragments.get(4));
                    break;
            }
        }
        ft.commit();
    }

    private void Click() {
        fragment.setListener(new homeFragment.onItemViewClickListener() {
            @Override
            public void onItemClick() {
                homeNavi.setSelectedItemId(R.id.btn_stare);
            }
            @Override
            public void onNewsClick() {
                homeNavi.setSelectedItemId(R.id.btn_news);
            }
        });

    }

}
